package com.yami.trading.api.controller;

import com.yami.trading.bean.model.C2cPaymentMethodConfig;
import com.yami.trading.bean.model.C2cTranslate;
import com.yami.trading.common.domain.Result;
import com.yami.trading.common.exception.BusinessException;
import com.yami.trading.common.util.StringUtils;
import com.yami.trading.service.AwsS3OSSFileService;
import com.yami.trading.service.c2c.C2cPaymentMethodConfigService;
import com.yami.trading.service.c2c.C2cTranslateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * C2C支付方式模板
 */
@RestController
public class ApiC2cPaymentMethodConfigController {
    @Autowired
    private C2cPaymentMethodConfigService c2cPaymentMethodConfigService;
    @Autowired
    private C2cTranslateService c2cTranslateService;
    @Autowired
    AwsS3OSSFileService awsS3OSSFileService;
    private final String action = "/api/c2cPaymentMethodConfig!";

    /**
     * 获取 支付方式模板 列表
     */
    @RequestMapping(action + "list.action")
    public Object list(HttpServletRequest request) {
        String language = request.getParameter("language");
        Map<String, String> methodConfigMap = this.c2cPaymentMethodConfigService.getMethodConfigMap();
        // 多语言
        for (String id : methodConfigMap.keySet()) {
            String name = methodConfigMap.get(id);
            if (null != name) {
                C2cTranslate trans = this.c2cTranslateService.get(name, language);
                if (null != trans) {
                    methodConfigMap.put(id, trans.getTranslate());
                }
            }
        }
        return Result.succeed(methodConfigMap);
    }

    /**
     * 获取 支付方式模板 详情
     */
    @RequestMapping(action + "get.action")
    public Object get(HttpServletRequest request) {
        String id = request.getParameter("id");
        String language = request.getParameter("language");
        C2cPaymentMethodConfig config = this.c2cPaymentMethodConfigService.getById(id);
        if (null == config) {
            throw new BusinessException("支付方式模板不存在");
        }
        if (StringUtils.isNotEmpty(config.getMethodImg())) {
            String path = awsS3OSSFileService.getUrl(config.getMethodImg());
            config.setMethodImg(path);
        }
        return Result.succeed(this.c2cTranslateService.translatePmc(config, language));
    }
}
